package clientGUI;

import banvetauclient.BanVeTauClient;
import banvetauclient.Serializer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import javax.swing.JPasswordField;

@SuppressWarnings("serial")
public class SignUp extends JFrame {

    private JPanel contentPane;
    private JTextField txtUser;
    private JTextField txtEmail;
    private JPasswordField txtPass;

    /**
     * Create the frame.
     */
    public SignUp() {
        setTitle("\u0110\u0103ng k\u00FD t\u00E0i kho\u1EA3n");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 364, 382);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblNewLabel = new JLabel("T\u00EAn t\u00E0i kho\u1EA3n:");
        lblNewLabel.setBounds(38, 54, 79, 14);
        contentPane.add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("M\u1EADt kh\u1EA9u:");
        lblNewLabel_1.setBounds(38, 126, 62, 14);
        contentPane.add(lblNewLabel_1);

        txtUser = new JTextField();
        txtUser.setBounds(127, 41, 197, 40);
        contentPane.add(txtUser);
        txtUser.setColumns(10);

        JButton btnSignUp = new JButton("\u0110\u0103ng k\u00FD");
        btnSignUp.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                RegAccount();
            }
        });
        btnSignUp.setBounds(127, 258, 91, 23);
        contentPane.add(btnSignUp);

        JButton btnExit = new JButton("Tho\u00E1t");
        btnExit.setBounds(233, 258, 91, 23);
        contentPane.add(btnExit);
        btnExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                CloseFrame();
            }
        });

        JLabel Email = new JLabel("Email:");
        Email.setBounds(38, 206, 62, 14);
        contentPane.add(Email);

        txtEmail = new JTextField();
        txtEmail.setColumns(10);
        txtEmail.setBounds(127, 193, 197, 40);
        contentPane.add(txtEmail);

        txtPass = new JPasswordField();
        txtPass.setBounds(127, 113, 197, 40);
        contentPane.add(txtPass);

    }

    private void RegAccount() {
        /*String username = txtUser.getText();
         //Check if user is exist	
         if(Login.AccountIsExist(username, null) == false)
         {
         Session session = TicketUtil.getSessionFactory().openSession();
         try
         {
         //Start the transaction
         session.beginTransaction();
         String pass = new String(txtPass.getPassword());
         String email = txtEmail.getText();
         //Check if email is exist	
         Query queryCheckEmail = session.createQuery("FROM Users where email = :email and is_active = 1");
         queryCheckEmail.setParameter("email", email);
         if(queryCheckEmail.list().size() > 0)//Email is exist
         {
         JOptionPane.showMessageDialog (null, "Email is exits. Please type the other email", "Notice", JOptionPane.ERROR_MESSAGE);
         }
         else
         {
         //Set information for new user
         Users newUser = new Users();
         newUser.setUsername(username);
         newUser.setPassword(pass);
         newUser.setEmail(email);
         newUser.setIsActive(1);
         newUser.setCreated(new Date());
         newUser.setActiveCode("ASXTNG");
         session.save(newUser);
					
         //Commit the transaction
         session.getTransaction().commit();
         }
				
         JOptionPane.showMessageDialog (null, "Register user successfull", "Notice", JOptionPane.INFORMATION_MESSAGE);
         this.dispose();
         }
         catch (HibernateException e) {
		         
         e.printStackTrace(); 
         }
         finally {
         session.close(); 
         }
         }
         else
         {
         JOptionPane.showMessageDialog (null, "Account is exits. Please type the other name", "Notice", JOptionPane.ERROR_MESSAGE);
         }*/

        try {
            if(Login.AccountIsExist(txtUser.getText(), null) == false)
            {
                //Send account's info to register to server
                String[] InfoAccount = new String[2];
                InfoAccount[0] = txtUser.getText();
                InfoAccount[1] = new String(txtPass.getPassword());
                InfoAccount[2] = txtEmail.getText();
                byte[] serialObject = Serializer.serialize(InfoAccount);

                BanVeTauClient.cos.write(1);
                BanVeTauClient.cos.write(serialObject.length);
                BanVeTauClient.cos.write(serialObject);
                BanVeTauClient.cos.flush();
                System.out.println("Sent info to server");
                
                //Confirm from server about register user
                boolean result = BanVeTauClient.cis.readBoolean();
                System.out.println(result);
                if(result)
                {
                    JOptionPane.showMessageDialog (null, "Register user successfull", "Notice", JOptionPane.INFORMATION_MESSAGE);
                }
                else
                {
                    JOptionPane.showMessageDialog (null, "Account is exits. Please type the other name", "Notice", JOptionPane.ERROR_MESSAGE);
                }
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void CloseFrame() {
        this.dispose();
    }
}
